package meta.api.web.data.businesscontrol;

import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import meta.api.web.common.business.BusiCmnService;
import meta.api.web.common.business.CmnService;
import meta.api.web.data.business.DataMngService;
import meta.api.web.data.presentation.form.FolderInfo;
import meta.api.web.directory.businessinfo.DirInfo;
import meta.common.exception.CmnBizException;

@Transactional
@Component
public class DataMngBusiCtrl {

    @Autowired
    private CmnService cmnService;

    @Autowired
    private BusiCmnService busiCmnService;

    @Autowired
    private DataMngService dataMngService;

    @Transactional(readOnly = true)
    public int getDataTotal(String userId, String collection, String type, String folder) {
        return dataMngService.getDataTotal(userId, collection, type, folder);
    }

    @Transactional(readOnly = true)
    public List<String> getDataList(String userId, String collection, String type, String folder,
            LinkedHashMap<String, Integer> projectionMap, Boolean projectionIncludeFlag,
            Integer current, Integer pageSize) {
        return dataMngService.getDataList(userId, collection, type, folder,
                projectionMap, projectionIncludeFlag, current, pageSize);
    }

    @Transactional(rollbackFor = Exception.class)
    public Long getSeqNextVal(String seqName) {
        return cmnService.getSeqNextVal(seqName);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveData(String id, String userId, String collection, String data,
            String owner, String editor) {
        dataMngService.saveData(id, userId, collection, data, owner, editor);
    }

    @Transactional(readOnly = true)
    public boolean checkIsDataOwner(String id, String userId) {
        return busiCmnService.checkIsDataOwner(id, userId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateData(String id, String userId, String data, String editor) {
        dataMngService.updateData(id, userId, data, editor);
    }

    @Transactional(readOnly = true)
    public String getData(String id, String userId,
            LinkedHashMap<String, Integer> projectionMap, Boolean projectionIncludeFlag) {
        return dataMngService.getData(id, userId, projectionMap, projectionIncludeFlag);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteData(String id, String userId) {
        dataMngService.deleteData(id, userId);
    }

    @Transactional(readOnly = true)
    public int getFolderListTotal(String userId, String type) {
        return dataMngService.getFolderListTotal(userId, type);
    }

    @Transactional(readOnly = true)
    public List<FolderInfo> getFolderList(String userId, String type) throws CmnBizException {
        return dataMngService.getFolderList(userId, type);
    }

    @Transactional(readOnly = true)
    public boolean checkExistFolder(String userId, String type, String fullpath) {
        return busiCmnService.checkExistDir(userId, type, fullpath);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addFolder(String userId, String type, String fullpath) {
        busiCmnService.addDirInfo(userId, type, fullpath);
    }

    @Transactional(readOnly = true)
    public boolean checkIsDirOwner(String id, String userId) {
        return busiCmnService.checkIsDirOwner(id, userId);
    }

    @Transactional(readOnly = true)
    public DirInfo getDir(String id, String userId) {
        return busiCmnService.getDirInfo(id, userId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateFolder(String userId, String collection,
            String oldFolderName, String newFolderName,
            List<Object> dataList, String editor) {
        dataMngService.updateFolder(userId, collection,
                oldFolderName, newFolderName, dataList, editor);
    }

    @Transactional(readOnly = true)
    public FolderInfo getFolder(String userId, String type, String fullpath) {
        return dataMngService.getFolder(userId, type, fullpath);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteFolder(String id, String userId) {
        busiCmnService.deleteDirInfo(id, userId);
    }
}
